package handler

import (
	"car-trade-mgr/cache"
	"car-trade-mgr/model"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
)

func CarConAdd(c *gin.Context) {
	carNo := c.PostForm("car_no")
	carName := c.PostForm("car_name")
	carCode := c.PostForm("car_code")
	if carNo == "" || carName == "" || carCode == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不为空",
		})
		return
	}
	car := model.CarCon{
		CarNo:   carNo,
		CarName: carName,
		CarCode: carCode,
	}
	err := model.DB.Create(&car).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	} else {
		c.JSON(200, gin.H{
			"code": 200,
			"msg":  "添加成功",
		})
		return
	}
}

func CarContractAdd(c *gin.Context) {
	carCo := c.PostForm("car_co")
	codeName := c.PostForm("code_name")
	codePrice, _ := strconv.Atoi(c.PostForm("code_price"))
	cid, _ := strconv.Atoi(c.PostForm("cid"))
	if carCo == "" || codeName == "" || codePrice == 0 || cid == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不为空",
		})
		return
	}
	carContract := model.CarContract{
		CarCo:     carCo,
		CodeName:  codeName,
		CodePrice: codePrice,
		Cid:       cid,
	}
	err := model.DB.Create(&carContract).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	} else {
		c.JSON(200, gin.H{
			"code": 200,
			"msg":  "添加成功",
		})
		return
	}
}

func CarContractList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "2"))
	offset := (page - 1) * size
	/*
			SELECT
		  car_contracts.id as CarContractId,
		  car_cons.car_no as CarNo,
		  car_cons.car_name as CarName,
		  car_cons.car_code as CarCode,
		  car_contracts.car_co as CarCo,
		  car_contracts.code_name as CodeName,
		  car_contracts.code_price as CodePrice
		FROM
		  car_contracts
		LEFT JOIN car_cons ON car_contracts.cid = car_cons.id;
	*/
	var list []model.CarContractList
	redis_key := "car_contract_list_" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		model.DB.Table("car_contracts").
			Select("car_contracts.id as CarContractId",
				"car_cons.car_no as CarNo",
				"car_cons.car_name as CarName",
				"car_cons.car_code as CarCode",
				"car_contracts.car_co as CarCo",
				"car_contracts.code_name as CodeName",
				"car_contracts.code_price as CodePrice").
			Joins("LEFT JOIN car_cons ON car_contracts.cid = car_cons.id").
			Offset(offset).
			Limit(size).
			Debug().
			Find(&list)
		jsonData, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &list)
	}
	var total int64
	model.DB.Model(&model.CarContract{}).Count(&total)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "查询成功",
		"total": total,
		"data":  list,
	})
	return
}

func CarContractDetail(c *gin.Context) {
	id := c.Query("id")
	var list model.CarContractList
	redis_key := "car_contract_detail_" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		model.DB.Table("car_contracts").
			Select("car_contracts.id as CarContractId",
				"car_cons.car_no as CarNo",
				"car_cons.car_name as CarName",
				"car_cons.car_code as CarCode",
				"car_contracts.car_co as CarCo",
				"car_contracts.code_name as CodeName",
				"car_contracts.code_price as CodePrice").
			Joins("LEFT JOIN car_cons ON car_contracts.cid = car_cons.id").
			Where("car_contracts.cid = ?", id).
			Debug().
			Find(&list)
		jsonData, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &list)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": list,
	})
	return
}
